import java.util.ArrayList;
import java.util.List;

class Solution {
    StringBuilder path;
    List<String> ret;
    String[] hash = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    public List<String> letterCombinations(String digits) {
        path = new StringBuilder();
        ret = new ArrayList<>();
        if (digits.isEmpty()) return ret;

        dfs(digits, 0);
        return ret;
    }

    private void dfs(String digits, int pos) {
        if (pos == digits.length()) {
            ret.add(path.toString());
            return;
        }

        String s = hash[digits.charAt(pos) - '0'];
        for (int i = 0; i < s.length(); i++) {
            path.append(s.charAt(i));
            dfs(digits, pos + 1);
            // 恢复现场
            path.deleteCharAt(path.length() - 1);
        }
    }
}